package com.lanxiao.cloud.modules.oauth.application;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.core.enums.StateEnum;
import com.lanxiao.cloud.common.core.service.IOperatorService;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.modules.oauth.application.assembler.PermissionAssembler;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionDto;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionTreeDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RolePermissionResultDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RouteDto;
import com.lanxiao.cloud.modules.oauth.domain.model.Permission;
import com.lanxiao.cloud.modules.oauth.domain.service.IPermissionService;
import com.lanxiao.cloud.modules.oauth.infrastructure.enums.TypeEnum;
import com.lanxiao.cloud.modules.oauth.infrastructure.repository.PermissionMapper;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-14
 */
@Component
public class PermissionQueryManager implements ILog<Permission, Long> {

  private final PermissionMapper permissionMapper;
  private final IPermissionService permissionService;
  private final PermissionAssembler permissionAssembler;
  private final IOperatorService operatorService;

  public PermissionQueryManager(
      PermissionMapper permissionMapper,
      IPermissionService permissionService,
      PermissionAssembler permissionAssembler,
      IOperatorService operatorService) {
    this.permissionMapper = permissionMapper;
    this.permissionService = permissionService;
    this.permissionAssembler = permissionAssembler;
    this.operatorService = operatorService;
  }

  /**
   *  条件查询结构树权限
   *
   * @author ：qiaoliang
   * @return java.util.List<com.lanxiao.cloud.modules.oauth.application.dto.PermissionTreeDto>
   * @date 2021-09-15 16:10
   */
  public List<PermissionTreeDto> treeQuery() {

    List<Permission> permissionList = permissionMapper.selectList(
        new LambdaQueryWrapper<Permission>()
            .eq(Permission::getDelFlag, DelFlagEnum.NOT_DELETE)
            .orderByAsc(Permission::getSort));

    return permissionAssembler.toPermissionTreeDto(permissionList);
  }

  /**
   *  查询非按钮类型的权限
   *
   * @author ：qiaoliang
   * @return java.util.List<com.lanxiao.cloud.modules.oauth.application.dto.PermissionTreeDto>
   * @date 2021-09-15 16:10
   */
  public List<PermissionDto> queryNotButtonType() {
    final LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.ne(Permission::getType, TypeEnum.BUTTON)
        .orderByAsc(Permission::getSort)
        .eq(Permission::getDelFlag, DelFlagEnum.NOT_DELETE);
    return permissionAssembler.toPermissionParentDto(permissionMapper.selectList(queryWrapper));
  }

  /**
   *  查询角色下的所有权限
   *
   * @author ：qiaoliang
   * @param roleId : 角色ID
   * @return java.util.List<com.lanxiao.cloud.modules.oauth.application.dto.RolePermissionDto>
   * @date 2021-09-16 11:47
   */
  public RolePermissionResultDto rolePermissionQuery(Long roleId) {
    return permissionAssembler.toRolePermissionDto(
        permissionMapper.selectList(new LambdaQueryWrapper<Permission>()
        .eq(Permission::getState, StateEnum.NORMAL)
        .orderByAsc(Permission::getSort)
        .eq(Permission::getDelFlag, DelFlagEnum.NOT_DELETE)),
        Objects.nonNull(roleId)?permissionMapper.findByRolePermission(roleId):Lists.newArrayList());
  }

  /**
   *  获取用户可访问资源
   *
   * @author ：qiaoliang
   * @return java.util.List<com.lanxiao.cloud.modules.oauth.application.dto.RouteDto>
   * @date 2021-09-29 14:33
   */
  public List<RouteDto> userRoute() {
    return permissionAssembler.toRouteDto(permissionService.getUserPermission(operatorService.operator()));
  }


  @Override
  public Permission selectById(Long id) {
    return permissionMapper.selectById(id);
  }

}
